﻿module Sustainalytics.Research {

    import Company = Sustainalytics.Company;

    export class ResearchPageController {

        public companyNameQueryString: string;
        public company: Company.CorporateDataExtendedDto;
        public $routeParams: any;

        constructor(
            protected $scope: Company.ICompanyBasedScope,
            protected $q: ng.IQService,
            protected $location: ng.ILocationService,
            protected RoutingService: Routing.IRoutingService,
            protected CompanyService: Company.ICompanyService) {

            var ctx = this;
            ctx.$routeParams = ctx.RoutingService.getRouteParams();
        }

        // init logic
        public initPage(): ng.IPromise<any> {
            var ctx = this;

            // get company name from route params
            var companyName: string = ctx.$routeParams['companyName'];
            ctx.companyNameQueryString = companyName;

            // get company by name
            var companyPromise = ctx.CompanyService.getByNameUrlFriendly(companyName);

            var def = ctx.$q.defer();

            companyPromise
                .then(
                // success
                function (company) {
                    ctx.company = company.data;
                    ctx.$scope.currentCompany = company.data;
                    def.resolve();
                },
                // error
                function (reason) {
                    def.reject(reason);
                });

            var promise = def.promise;

            return promise;
        }
    }


    angular.module('Sustainalytics.Research').controller('ResearchPageController',
        ['$scope', '$q', '$location', 'RoutingService', 'CompanyService', ResearchPageController]);
}